<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/common/taglib.jsp"%>
<!DOCTYPE html>
<html>
<head>
<link href="${ctx}resources/css/select/bootstrap-select.css" rel="stylesheet" type="text/css">
<%@ include file="/WEB-INF/common/head.jsp"%>
</head>
<body class="${cookie.bodyClass.value}">
	<div class="container body">
		<div class="main_container">
			<%@ include file="/WEB-INF/common/left.jsp"%>
			<%@ include file="/WEB-INF/common/top.jsp"%>
			<!-- page content -->
			<div class="right_col" role="main" id="main">
				<div class="x_panel">
					<div class="x_title">
						<h2>商品详情</h2>
						<div class="clearfix"></div>
					</div>
					<div class="x_content">
						<div class="modal-body">
							<form role="form" data-parsley-validate id="commodityForm" class="form-horizontal">
								<input id="commodityId" name="commodityId" type="hidden" value="${commodity.commodityId}" />
								<input id="imgs" name="imgs" type="hidden" value="${commodity.imgs}" />
								<div class="form-group item">
									<label for="name" class="col-sm-3 control-label">商品名称:<font color="red">*</font>
									</label>
									<div class="col-sm-6">
										<input type="text" class="form-control" id="commodityName" name="commodityName" value="${commodity.commodityName}" placeholder="商品名称"
											required='required' data-parsley-length="[0,40]">
									</div>
								</div>
								<div class="form-group item">
									<label for="name" class="col-sm-3 control-label">商品说明:<font color="red">*</font>
									</label>
									<div class="col-sm-6">
										<textarea id="remarks" name="remarks" rows="7" cols="70" style="resize: none;" class="form-control" placeholder="商品说明" required="required">${commodity.remarks}</textarea>
									</div>
								</div>
								<div class="form-group item">
									<label for="name" class="col-sm-3 control-label">所属商户:<font color="red">*</font>
									</label>
									<div class="col-sm-6">
										<select id="merchantId" name="merchantId" class="form-control"  required="required" >
										</select>
									</div>
								</div>
								<div class="form-group item">
									<label for="name" class="col-sm-3 control-label">所属分类:<font color="red">*</font>
									</label>
									<div class="col-sm-3">
										<select id="firstCategoryId" name="firstCategoryId" class="form-control"  required="required" >
										</select>
									</div>
									<div class="col-sm-3">
										<select id="secondCategoryId" name="secondCategoryId" class="form-control"  required="required" >
										</select>
									</div>
								</div>
								<div class="form-group item">
									<label for="address" class="col-sm-3 control-label">一口价:<font color="red">*</font>
									</label>
									<div class="col-sm-6">
										<input type="text" class="form-control" id="commodityPrice" name="commodityPrice" value="${commodity.commodityPrice}" placeholder="一口价"
											required='required' data-parsley-length="[0,40]" data-parsley-pattern="[-\+]?\d+(\.\d+)?">
									</div>
								</div>
								<div class="form-group item">
									<label for="address" class="col-sm-3 control-label">商品规格:<font color="red">*</font> <input type="hidden" id="specificationCount" name="specificationCount"
										value="0" />
									</label>
									<div class="col-sm-6">
										<button type="button" class="btn btn-primary btn-sm" onclick="addSpecificationModal();">新增规格</button>
										<table class="table table-bordered" id="specificationTable" style="display: none;">
											<thead>
												<tr>
													<th>规格图片</th>
													<th>规格</th>
													<th>价格</th>
													<th>数量</th>
													<th>积分</th>
													<th>操作</th>
												</tr>
											</thead>
											<tbody id="specificationTbody">
											</tbody>
										</table>
									</div>
								</div>
								<div class="form-group item">
									<label for="address" class="col-sm-3 control-label">实际销售数量:<font color="red">*</font>
									</label>
									<div class="col-sm-6">
										<input type="text" class="form-control" id="realBuyCount" name="realBuyCount" value="${commodity.realBuyCount}" placeholder="实际销售数量"
											 disabled="disabled">
									</div>
								</div>
								<div class="form-group item">
									<label for="address" class="col-sm-3 control-label">展示销售数量:<font color="red">*</font>
									</label>
									<div class="col-sm-6">
										<input type="text" class="form-control" id="buyCount" name="buyCount" value="${commodity.buyCount}" placeholder="展示销售数量" required='required'
											data-parsley-length="[0,40]" data-parsley-type="integer">
									</div>
								</div>
								<div class="form-group item">
									<label for="address" class="col-sm-3 control-label">商品运费:<font color="red">*</font>
									</label>
									<div class="col-sm-6">
										<input type="text" class="form-control" id="freight" name="freight" value="${commodity.freight}" placeholder="商品运费" required='required'
											data-parsley-length="[0,40]" data-parsley-pattern="[-\+]?\d+(\.\d+)?">
									</div>
								</div>
								<div class="form-group item">
								<label for="address" class="col-sm-3 control-label">是否上架:<font color="red">*</font>
								</label>
								<div class="col-sm-6">
										<span>
											<input type="radio" class="input-xsmall" id="not_Shelves" name="isShelves" value="0" checked>否
											<input type="radio" class="input-xsmall" id="is_Shelves" name="isShelves" value="1" >是
										</span>
								</div>
							</div>
							<div class="form-group item">
								<label for="address" class="col-sm-3 control-label">是否推荐:<font color="red">*</font>
								</label>
								<div class="col-sm-6">
										<span>
											<input type="radio" class="input-xsmall" id="not_Recommend" name="isRecommend" value="0"  checked>否
											<input type="radio" class="input-xsmall" id="is_Recommend" name="isRecommend" value="1" >是
										</span>
								</div>
							</div>
							<div class="form-group item">
								<label for="address" class="col-sm-3 control-label">是否是积分兑换商品:<font color="red">*</font>
								</label>
								<div class="col-sm-6">
										<span>
											<input type="radio" class="input-xsmall" id="not_PointsExchange" name="isPointsExchange" value="0" checked>否
											<input type="radio" class="input-xsmall" id="is_PointsExchange" name="isPointsExchange" value="1">是
										</span>
								</div>
							</div>
							<div class="form-group item">
								<label for="address" class="col-sm-3 control-label">积分返利倍数:<font color="red">*</font>
								</label>
								<div class="col-sm-6">
									<input type="text" class="form-control" id="multiple" name="multiple" value="${commodity.multiple}" placeholder="积分返利倍数" required='required'
										   data-parsley-length="[0,40]" data-parsley-pattern="[-\+]?\d+(\.\d+)?" >
								</div>
							</div>
								<div class="form-group item">
									<label for="url" class="col-sm-3 control-label">商品主图:<font color="red">*</font>
									</label>
									<div class="col-sm-6">
										<input type="hidden" id="imageUrl" name="imageUrl" value="${commodity.imageUrl}" /> <img src="${ctx}resources/images/zanwutupian.jpg"
											id="showImage" width="100px;" height="100px;">
										<button type="button" class="btn btn-primary btn-sm" style="display: block;width: 100px;margin-top: 10px;">上传图片</button>
										<input type="file" id="fileLogo" name="file" class="hideFile" onchange="uploadImage(this)" />
										<font color="red"><!-- 建议图片比例上传1:1的图片 -->
											建议上传200*200大小的图片，仅支持jpg，png等格式
										</font>
									</div>
								</div>
								<div class="form-group item">
									<label for="url" class="col-sm-3 control-label">商品详请图片:<font color="red">*</font>
									</label>
									<div class="col-sm-6" id="imageDiv" style="float: left;">
										<div style="float: left;position:relative;" id="imgP">
											<input type="hidden" id="imageUrls" /> <img src="${ctx}resources/images/zanwutupian.jpg" id="showImageUrl" width="200px;" height="200px;" />
											<div>
												<button type="button" class="btn btn-primary btn-sm" style="display: block;width: 100px;margin-top: 10px;float: left;">上传图片</button>
												<button type="button" class="btn btn-danger btn-sm" style="display: block;width: 100px;margin-top: 10px;float: left;" onclick="delImg('')">删除</button>
												<input type="file" id="file" name="file" class="hideFiles" onchange="uploadImages(this,'')" />
											</div>
											<div style="width:200px;">
											<font color="red"><!-- 建议图片比例上传1:1的图片 -->
												建议上传500*500大小的图片，仅支持jpg，png等格式
											</font>
											</div>
										</div>
									</div>
								</div>
								<div class="form-group item">
									<label for="url" class="col-sm-3 control-label">商品详情:<font color="red">*</font>
									</label>
									<div class="col-sm-8" style="padding-top: 5.5px;">
										<textarea id="description" name="description" required='required' >${commodity.description}</textarea>
									</div>
								</div>
								<div class="form-group item">
									<label for="url" class="col-sm-3 control-label">商品参数:<font color="red">*</font>
									</label>
									<div class="col-sm-8" style="padding-top: 5.5px;">
										<textarea id="parameter" name="parameter" required='required' >${commodity.parameter}</textarea>
									</div>
								</div>
							</form>
						</div>
						<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12" style="text-align:center">
							<button type="button" class="btn btn-dark btn-sm" onclick="back();">返回</button>
							<button type="button" class="btn btn-primary btn-sm" onclick="save();">保存</button>
						</div>
					</div>
				</div>
			</div>
			<!-- /page content -->
			<%@ include file="/WEB-INF/common/foot.jsp"%>
		</div>
	</div>
	<div id="specificationModal" class="modal fade" tabindex="-1" role="dialog" data-backdrop="false" aria-hidden="true">
		<div class="modal-dialog modal-md">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-label="Close">
						<span aria-hidden="true">×</span>
					</button>
					<h4 class="modal-title" id="modelTitle">新增商品规格</h4>
				</div>
				<div class="modal-body">
					<form role="form" data-parsley-validate id="specificationForm" class="form-horizontal">
						<div class="form-group item">
							<label for="fullName2" class="col-sm-3 control-label">规格图片：</label>
							<div class="col-sm-6">
								<div style="float: left;position:relative;" id="imgP">
									<input type="hidden" id="specificationImage" /> <img src="${ctx}resources/images/zanwutupian.jpg" id="showSpecificationImage"
										width="100px;" height="100px;" />
									<div>
										<button type="button" class="btn btn-primary btn-sm" style="display: block;width: 100px;margin-top: 10px;">上传图片</button>
										<input type="file" id="specificationFile" name="file" class="hideFile" onchange="uploadSpecificationImage(this,'')" />
									</div>
									<font color="red"><!-- 建议图片比例上传1:1的图片 -->
										建议上传100*100大小的图片，仅支持jpg，png等格式
									</font>
								</div>
							</div>
						</div>
						<div class="form-group item">
							<label for="username2" class="col-sm-3 control-label">规格：</label>
							<div class="col-sm-6">
								<input type="text" class="form-control" id="specificationName" name="specificationName" placeholder="规格" required="required"
									data-parsley-length="[0,40]">
							</div>
						</div>
						<div class="form-group item">
							<label for="password1" class="col-sm-3 control-label">价格：</label>
							<div class="col-sm-6">
								<input type="text" class="form-control" id="currentPrice" name="currentPrice" placeholder="价格" required="required"
									data-parsley-length="[0,40]" data-parsley-pattern="[-\+]?\d+(\.\d+)?">
							</div>
						</div>
						<div class="form-group item">
							<label for="password2" class="col-sm-3 control-label">数量：</label>
							<div class="col-sm-6">
								<input type="text" class="form-control" id="supplyCount" name="supplyCount" placeholder="数量" required="required" data-parsley-length="[0,40]"
									data-parsley-type="integer">
							</div>
						</div>
						<div class="form-group item">
							<label for="password2" class="col-sm-3 control-label">积分：</label>
							<div class="col-sm-6">
								<input type="text" class="form-control" id="points" name="points" placeholder="积分" required="required" data-parsley-length="[0,40]"
									data-parsley-pattern="[-\+]?\d+(\.\d+)?">
							</div>
						</div>
					</form>
				</div>
				<div class="modal-footer">
					<input type="hidden" id="operate" value="0" /> <input type="hidden" id="indexEdit" />
					<button type="button" class="btn btn-default" onclick="closeModal();">取消</button>
					<button type="button" class="btn btn-success" onclick="addSpecification();">保存</button>
				</div>
			</div>
		</div>
	</div>
	<%@ include file="/WEB-INF/common/script.jsp"%>
	<script type="text/javascript" src="${ctx}resources/js/ajaxfileupload.js"></script>
	<script src="${ctx}resources/js/select/bootstrap-select.js"></script>
	<script type="text/javascript">
	var editor1;
	KindEditor.ready(function(K) {
		editor1= K.create('#description', {
			width: '99.9%',
		    height: '440px',
		    urlType :'ctx',
			cssPath :ctx+ '/resources/js/editor/plugins/code/prettify.css',
			langType : 'zh_CN',
			uploadJson : ctx + 'admin/upload/uploadManager',
			fileManagerJson : ctx + 'admin/upload/fileManager',
			allowFileManager : true,
		    resizeType : 0,  //文本框不可拖动
		});
	});
	var editor2;
	KindEditor.ready(function(K) {
		editor2 = K.create('#parameter', {
			width: '99.9%',
		    height: '440px',
		    urlType :'ctx',
			cssPath :ctx+ '/resources/js/editor/plugins/code/prettify.css',
			langType : 'zh_CN',
			uploadJson : ctx + 'admin/upload/uploadManager',
			fileManagerJson : ctx + 'admin/upload/fileManager',
			allowFileManager : true,
		    resizeType : 0,  //文本框不可拖动
		});
	});
	</script>
	<script type="text/javascript">
		function getImageSize() {
			var imageSize = 0;
			$("input[name='imageUrls']").each(function(index, item) {
				if ($(this).val() != "") {
					imageSize++;
				}
			});
			return imageSize;
		}
		function appendNewImg(src, img, currentIndex) {
			var html = '<div style="float: left;position:relative;" id="imgP'+currentIndex+'">';
			html += '<input type="hidden" name="imageUrls" id="imageUrls'+currentIndex+'" value="'+img+'"/> ';
			html += '<img src="'+src+'" id="showImage'+currentIndex+'" width="200px;" height="200px;">';
			html += '<div>';
			html += '<button type="button" class="btn btn-primary btn-sm" style="display: block;width: 100px;margin-top: 10px;float: left;">上传图片</button>';
			html += '<button type="button" class="btn btn-danger btn-sm" style="display: block;width: 100px;margin-top: 10px;float: left;" onclick="delImg('
					+ currentIndex + ')">删除</button> ';
			html += '<input type="file" id="file'
					+ currentIndex
					+ '" name="file" class="hideFiles" onchange="uploadImages(this,'
					+ currentIndex + ')" />';
			html += '</div>';
			html += '</div>';
			$("#imgP").before(html);
			var imageSize = getImageSize();
			if (imageSize == 6) {
				$("#imgP").hide();
			}
		}
		function delImg(index) {
			if ($("#imageUrls" + index).val() == '') {
				return;
			}
			$("#imgP" + index).remove();
			var imageSize = getImageSize();
			if (imageSize < 6) {
				$("#imgP").show();
			}
		}
		function uploadImages(obj, index) {
			var fileId = $(obj).attr("id");
			/* swal({
				"title" : "",
				"text" : "正在处理……请稍后",
				"showConfirmButton" : false
			}); */
			$.ajaxFileUpload({
				url : ctx + "admin/upload/uploadImg?dir=commodity", //用于文件上传的服务器端请求地址
				secureuri : false, //是否需要安全协议，一般设置为false
				fileElementId : fileId, //文件上传域的ID
				dataType : 'json', //返回值类型 一般设置为json
				success : function(data, status) //服务器成功响应处理函数
				{
					data = $.parseJSON(data);
					if (data.status == 0) {
						var src = data.data.src;
						var img = data.data.img;
						if ($("#imageUrls" + index).val() == "") {
							appendNewImg(src, img, (new Date().getTime()));
						} else {
							$("#showImage" + index).attr("src", src);
							$("#imageUrls" + index).val(img);
						}
						/* swal.close(); */
					} else {
						swal({
							title : "",
							text : data.msg,
							type : "error"
						});
					}
				},
				error : function(data, status, e)//服务器响应失败处理函数
				{
					swal({
						title : "",
						text : data.msg,
						type : "error"
					});
				}
			});
		}
		function uploadImage(obj) {
			var fileId = $(obj).attr("id");
			/* 	swal({
					"title" : "",
					"text" : "正在处理……请稍后",
					"showConfirmButton" : false
				}); */
			$.ajaxFileUpload({
				url : ctx + "admin/upload/uploadImg?dir=commodity", //用于文件上传的服务器端请求地址
				secureuri : false, //是否需要安全协议，一般设置为false
				fileElementId : fileId, //文件上传域的ID
				dataType : 'json', //返回值类型 一般设置为json
				success : function(data, status) //服务器成功响应处理函数
				{
					data = $.parseJSON(data);
					if (data.status == 0) {
						$("#showImage").attr("src", data.data.src);
						$("#imageUrl").val(data.data.img);
						swal.close();
					} else {
						swal({
							title : "",
							text : data.msg,
							type : "error"
						});
					}
				},
				error : function(data, status, e)//服务器响应失败处理函数
				{
					swal({
						title : "",
						text : data.msg,
						type : "error"
					});
				}
			});
		}
		function closeModal() {
			$('#specificationModal').modal("hide");
		}
		function addSpecificationModal() {
			$("#operate").val("0");
			$("#indexEdit").val("");
			$('#specificationForm').reset();
			$('#specificationModal').modal("show");
		}
		function edidSpecification(index) {
			$("#operate").val("1");
			$("#indexEdit").val(index);
			$('#specificationForm').reset();
			$('#specificationModal').modal("show");
			$("#specificationImage").val(
					$("#specificationImageUrl" + index).val());
			$("#showSpecificationImage").attr("src",
					imgDomain + "/" + $("#specificationImageUrl" + index).val());
			$("#specificationName").val($("#specificationName" + index).val());
			$("#currentPrice").val($("#currentPrice" + index).val());
			$("#supplyCount").val($("#supplyCount" + index).val());
			$("#points").val($("#points" + index).val());
		}
		function addSpecification() {
			if (!$('#specificationForm').parsley().validate()) {
				return;
			}
			var operate = $("#operate").val();
			var specificationImage = $("#specificationImage").val();
			var specificationName = $("#specificationName").val();
			var currentPrice = $("#currentPrice").val();
			var supplyCount = $("#supplyCount").val();
			var points = $("#points").val();
			if (operate == "0") {
				var index = new Date().getTime();
				var html = '<tr id="tr'+index+'">';
				html += '<th scope="row"><input type="hidden" id="specificationImageUrl'+index+'" name="specificationImageUrls" value="'+specificationImage+'"/><img src="${imgDomain}'+'/'+specificationImage+'" id="showSpecificationImageUrl'+index+'" width="100px;" height="50px;" /></th>';
				html += '<td><span id="specificationNameSpan'+index+'">'
						+ specificationName
						+ '</span><input type="hidden" class="form-control" id="specificationName'+index+'" name="specificationNames" value="'+specificationName+'"/></td>';
				html += '<td><span id="currentPriceSpan'+index+'">'
						+ currentPrice
						+ '</span><input type="hidden" class="form-control" id="currentPrice'+index+'" name="specificationCurrentPrices"  value="'+currentPrice+'"/></td>';
				html += '<td><span id="supplyCountSpan'+index+'">'
						+ supplyCount
						+ '</span><input type="hidden" class="form-control" id="supplyCount'+index+'" name="specificationSupplyCounts"  value="'+supplyCount+'"/></td>';
				html += '<td><span id="pointsSpan'+index+'">'
						+ points
						+ '</span><input type="hidden" class="form-control" id="points'+index+'" name="specificationPointss"  value="'+points+'"/></td>';
				html += '<td><button type="button" class="btn btn-dark btn-sm" onclick="delSpecification('
						+ index
						+ ');">删除</button> <button type="button" class="btn btn-primary btn-sm" onclick="edidSpecification('
						+ index + ');">编辑</button></td>';
				html += '</tr>';
				$("#specificationTbody").append(html);
				var specificationCount = parseInt($("#specificationCount")
						.val());
				$("#specificationCount").val(specificationCount + 1);
				if (parseInt($("#specificationCount").val()) > 0) {
					$("#specificationTable").show();
				}
			} else {
				var index = $("#indexEdit").val();
				$("#showSpecificationImageUrl" + index).attr("src",
						imgDomain + "/" + specificationImage);
				$("#specificationNameSpan" + index).text(specificationName);
				$("#currentPriceSpan" + index).text(currentPrice);
				$("#supplyCountSpan" + index).text(supplyCount);
				$("#pointsSpan" + index).text(points);
				$("#specificationImageUrl" + index).val(specificationImage);
				$("#specificationName" + index).val(specificationName);
				$("#currentPrice" + index).val(currentPrice);
				$("#supplyCount" + index).val(supplyCount);
				$("#points" + index).val(points);
			}
			closeModal();
		}
		function delSpecification(index) {
			$("#tr" + index).remove();
			var specificationCount = parseInt($("#specificationCount").val());
			$("#specificationCount").val(specificationCount - 1);
			if (parseInt($("#specificationCount").val()) <= 0) {
				$("#specificationTable").hide();
			}
		}
		function uploadSpecificationImage(obj) {
			var fileId = $(obj).attr("id");
			/* swal({
				"title" : "",
				"text" : "正在处理……请稍后",
				"showConfirmButton" : false
			}); */
			$
					.ajaxFileUpload({
						url : ctx + "admin/upload/uploadImg?dir=commodity", //用于文件上传的服务器端请求地址
						secureuri : false, //是否需要安全协议，一般设置为false
						fileElementId : fileId, //文件上传域的ID
						dataType : 'json', //返回值类型 一般设置为json
						success : function(data, status) //服务器成功响应处理函数
						{
							data = $.parseJSON(data);
							if (data.status == 0) {
								$("#showSpecificationImage").attr("src",
										data.data.src);
								$("#specificationImage").val(data.data.img)
								/* swal.close(); */
							} else {
								swal({
									title : "",
									text : data.msg,
									type : "error"
								});
							}
						},
						error : function(data, status, e)//服务器响应失败处理函数
						{
							swal({
								title : "",
								text : data.msg,
								type : "error"
							});
						}
					});
		}
		function showImage() {
			if ( $("#imgs").val()!="") {
				var imageUrls = $("#imgs").val().split(",");
				$.each(imageUrls, function(index, item) {
					var index = new Date().getTime();
					var img = item;
					appendNewImg(imgDomain + "/" + img, img, index);
				});
			}
		}
		function showSpecification(){
			$("#specificationCount").val("0");
		    <c:forEach items="${commodity.specifications}" var="item" varStatus="status" >  
		       /*  alert("${status.count}");  //获得其下标 
		        alert("${item.specificationName}");   //传递过来的是字符串，加引号 、 */
		        var specificationId="${item.specificationId}";
		        var specificationImage="${item.imageUrl}";
		        var specificationName="${item.specificationName}";
		        var currentPrice="${item.currentPrice}";
		        var supplyCount="${item.supplyCount}";
		        var points="${item.points}";
		        var index = new Date().getTime();
				var html = '<tr id="tr'+index+'">';
				html += '<input type="hidden" id="specificationId'+index+'" name="specificationIds" value="'+specificationId+'"/>';
				html += '<th scope="row"><input type="hidden" id="specificationImageUrl'+index+'" name="specificationImageUrls" value="'+specificationImage+'"/><img src="${imgDomain}'+'/'+specificationImage+'" id="showSpecificationImageUrl'+index+'" width="100px;" height="50px;" /></th>';
				html += '<td><span id="specificationNameSpan'+index+'">'
						+ specificationName
						+ '</span><input type="hidden" class="form-control" id="specificationName'+index+'" name="specificationNames" value="'+specificationName+'"/></td>';
				html += '<td><span id="currentPriceSpan'+index+'">'
						+ currentPrice
						+ '</span><input type="hidden" class="form-control" id="currentPrice'+index+'" name="specificationCurrentPrices"  value="'+currentPrice+'"/></td>';
				html += '<td><span id="supplyCountSpan'+index+'">'
						+ supplyCount
						+ '</span><input type="hidden" class="form-control" id="supplyCount'+index+'" name="specificationSupplyCounts"  value="'+supplyCount+'"/></td>';
				html += '<td><span id="pointsSpan'+index+'">'
						+ points
						+ '</span><input type="hidden" class="form-control" id="points'+index+'" name="specificationPointss"  value="'+points+'"/></td>';
				html += '<td><button type="button" class="btn btn-dark btn-sm" onclick="delSpecification('
						+ index
						+ ');">删除</button> <button type="button" class="btn btn-primary btn-sm" onclick="edidSpecification('
						+ index + ');">编辑</button></td>';
				html += '</tr>';
				$("#specificationTbody").append(html);
				var specificationCount = parseInt($("#specificationCount")
						.val());
				$("#specificationCount").val(specificationCount + 1);
				if (parseInt($("#specificationCount").val()) > 0) {
					$("#specificationTable").show();
				}
		    </c:forEach>   
		}
		$(function() {
		 	initMerchantId();
			initFirstCategoryId();
		 	initSecondCategoryId();
			$("#firstCategoryId").change(function(){
				initSecondCategoryId();
			});
			
			var commodityId = $("#commodityId").val();
			if (commodityId != "") {
				$("#merchantId").val("${commodity.merchantId}");
				$("#firstCategoryId").val("${commodity.firstCategoryId}");
			 		$("#firstCategoryId").change();
				 $("#secondCategoryId").val("${commodity.secondCategoryId}");
				 $("input[name='isShelves'][value=${commodity.isShelves}]").attr("checked",true); 
				 $("input[name='isPointsExchange'][value=${commodity.isPointsExchange}]").attr("checked",true); 
				 $("input[name='isRecommend'][value=${commodity.isRecommend}]").attr("checked",true); 
				$("#showImage").attr("src",
						'${imgDomain}' + '/' + '${commodity.imageUrl}');
				$("#imageUrl").val('${commodity.imageUrl}');
				showImage();
				showSpecification();
			}
		});
		
		function initMerchantId(){
			$.ajax({
				url : ctx + "admin/commodity/findMerchantList",
				type : 'post',
				data : {},
				dataType : "json",
				async : false,
				success : function(data) {
					//alert(JSON.stringify(data)); 
					var contentHTML = "<option value=''>请选择</option>";
					$.each(data, function(i, item) {
						contentHTML += "<option value='"+item.merchantId+"'>"
								+ item.merchantName + "</option>";
					});
					$("#merchantId").html(contentHTML);
				}
			});
		}
		function initFirstCategoryId(){
			$.ajax({
				url : ctx + "admin/commodity/findCategoryList",
				type : 'post',
				data : {"level":0},
				dataType : "json",
				async : false,
				success : function(data) {
					//alert(JSON.stringify(data)); 
					var contentHTML = "<option value=''>请选择</option>";
					$.each(data, function(i, item) {
						contentHTML += "<option value='"+item.categoryId+"'>"
								+ item.categoryName + "</option>";
					});
					$("#firstCategoryId").html(contentHTML);
				}
			});
		}
		function initSecondCategoryId(){
			var firstCategoryId=$("#firstCategoryId").val();
			if (firstCategoryId!="") {
				$.ajax({
					url : ctx + "admin/commodity/findCategoryList",
					type : 'post',
					data : {"level":"1","parent":firstCategoryId},
					dataType : "json",
					async : false,
					success : function(data) {
						//alert(JSON.stringify(data)); 
						var contentHTML = "<option value=''>请选择</option>";
						$.each(data, function(i, item) {
							contentHTML += "<option value='"+item.categoryId+"'>"
							+ item.categoryName + "</option>";
						});
						$("#secondCategoryId").html(contentHTML);
					}
				});
			}else{
				var contentHTML = "<option value=''>请选择</option>";
				$("#secondCategoryId").html(contentHTML);
			}
		}
		function back() {
			window.location = ctx + "admin/commodity/toCommodityPage";
		}

		function save() {
			editor1.sync();
			editor2.sync();
			if (!$('#commodityForm').parsley().validate()) {
				return;
			}
/* 			if($("#parameter").html()!=""){
				swal("","商品参数不能为空","error");
				return;
			} */
			swal({
				"title" : "",
				"text" : "正在处理……请稍后",
				"showConfirmButton" : false
			});
			$.ajax({
				url : ctx + "admin/commodity/saveCommodity?time="+ (new Date()).getTime(),
				dataType : "json",
				data : $("#commodityForm").serialize(),
				success : function(result) {
					if (result.status == "0") {
						swal({
							title : '',
							text : "保存成功"
						}, function() {
							back();
						});
					} else {
						swal({
							title : '',
							text : result.msg,
							type : "error"
						});
					}
				}
			});
		}
	</script>
</body>
</html>